package com.eascs.workflow.sys.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.dao.BaseDao;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.entity.sys.ActOrgRoleUserEntity;
import com.eascs.workflow.common.entity.sys.SysRole;
import com.eascs.workflow.common.entity.sys.SysUser;
import com.eascs.workflow.common.vo.OrgUserRoleVo;
import com.eascs.workflow.sys.dao.SysOrgRoleUserDao;

@Repository
public class SysOrgRoleUserDaoImpl extends BaseDao implements SysOrgRoleUserDao {
	@Override
	public PageResult<OrgUserRoleVo> searchOrgUserRolePage(Map<String, String> param, int pageSize, int pageIndex) {
		StringBuffer sql = new StringBuffer();
		sql.append(
				"SELECT r.id id,employee_namecn userName,employee_no userNo, o.org_name orgName,r.role_name roleName,employee_account account,e.mobile");
		sql.append(" FROM o2o_employee e,act_org_role_user r, act_org o");
		sql.append(" WHERE e.employee_no = r.user_no and r.org_id=o.id and r.is_remove =0 ");
		List<Object> paramsList = new ArrayList<>();
		if (StringUtils.isNotEmpty(param.get("userName"))) {
			sql.append(" and employee_namecn like ?");
			paramsList.add("%" + param.get("userName") + "%");
		}
		if (StringUtils.isNotEmpty(param.get("account"))) {
			sql.append(" and e.employee_account = ?");
			paramsList.add(param.get("account"));
		}
		if (StringUtils.isNotEmpty(param.get("orgId"))) {
			sql.append(" and o.id = ?");
			paramsList.add(param.get("orgId"));
		}
		if (StringUtils.isNotEmpty(param.get("orgName"))) {
			sql.append(" and o.org_name = ?");
			paramsList.add(param.get("orgName"));
		}
		Object[] params = paramsList.toArray();
		SqlPageQuery<OrgUserRoleVo> sqlPageQuery = new SqlPageQuery<>(OrgUserRoleVo.class, pageSize, pageIndex,
				sql.toString());
		sqlPageQuery.addParams(params);
		return super.findDataWithPage(sqlPageQuery);
	}

	@Override
	public Integer saveOrUpdateOrgUserRole(ActOrgRoleUserEntity orgRoleUserEntity) {
		if (StringUtils.isNotEmpty(orgRoleUserEntity.getId())) {
			return workflowJdbcSession.updateById(orgRoleUserEntity);
		} else {
			return workflowJdbcSession.insert(orgRoleUserEntity);
		}
	}

	@Override
	public Boolean checkExist(String userNo, String roleId, String orgId) {
		String sql = "SELECT count(*) from act_org_role_user where is_remove=0 and user_no = ? and role_id = ? and org_id = ?";
		int count = super.findInteger(sql, userNo, roleId, orgId);
		if (count > 0) {
			return true;
		}
		return false;
	}

	@Override
	public Integer deleteById(String id) {
		String sql = "UPDATE act_org_role_user SET is_remove = 1 where ID=?";
		return super.execute(sql, id);
	}

	@Override
	public List<ActOrgRoleUserEntity> getUserRoleList(String userNo) {
		String sql = "SELECT * FROM act_org_role_user r WHERE r.is_remove=0 AND  r.user_no=? and r.is_remove = 0";
		return super.findEntitys(sql, ActOrgRoleUserEntity.class, userNo);
	}

	@Override
	public PageResult<Object[]> selectOrgRoleUserPage(ActOrgRoleUserEntity obj, int pageSize, int pageIndex) {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT id,user_no,user_name,org_name,role_name FROM act_org_role_user");
		sql.append(" WHERE 1=1 and is_remove = 0");
		List<Object> paramsList = new ArrayList<>();
		if (StringUtil.hasText(obj.getRoleId())) {
			sql.append(" and role_id=?");
			paramsList.add(obj.getRoleId());
		}
		if (StringUtil.hasText(obj.getUserName())) {
			sql.append(" and user_name=?");
			paramsList.add(obj.getUserName());
		}
		Object[] params = paramsList.toArray();
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<Object[]>(Object[].class, pageSize, pageIndex,
				sql.toString());
		pageQuery.addParams(params);
		return workflowJdbcSession.findDataWithPage(pageQuery);
	}

	@Override
	public Integer insertOrgUserRoleByempno(User user,String employeeNo,String roleId, String orgId) {
		if(employeeNo==null || roleId==null){
			return 0;
		}
		String selectEmpSql = "SELECT * FROM o2o_employee WHERE employee_no=?";
		String selectUnique = "SELECT COUNT(*) FROM act_org_role_user WHERE org_id=? AND role_id=? AND user_no=? and is_remove = 0";
		SysRole role = workflowJdbcSession.getById(SysRole.class, roleId);
		SysUser employee = workflowJdbcSession.findEntity(selectEmpSql, SysUser.class, employeeNo);
		if(employee.getOperationDept()==null){
			return 0;
		}
		ActOrgEntity org = workflowJdbcSession.getById(ActOrgEntity.class, orgId);
		if( org == null){
			return -1;
		}
		int count = workflowJdbcSession.findInteger(selectUnique, new Object[]{org.getOrgId(),roleId,employeeNo});
		if( count > 0){
			return -2;
		}
		ActOrgRoleUserEntity orgRoleUser=new ActOrgRoleUserEntity();
		orgRoleUser.setOrgId(org.getOrgId());
		orgRoleUser.setOrgName(org.getOrgName());
		orgRoleUser.setRoleId(roleId);
		orgRoleUser.setRoleName(role.getRoleName());
		orgRoleUser.setUserNo(employeeNo);
		orgRoleUser.setUserName(employee.getEmployeeNamecn());
		orgRoleUser.setIsRemove(0);
		orgRoleUser.setCreateTime(new Date());
		orgRoleUser.setCreator(user.getAccount());
		return workflowJdbcSession.insert(orgRoleUser);
	}

	@Override
	public List<ActOrgRoleUserEntity> getUserRoleListWithOrgId(String userNo, String orgId) {
		String sql = "SELECT oru.org_id, r.role_name from act_org_role_user oru,o2o_act_role r where oru.role_id = r.id "
				+ "and oru.user_no = ? and oru.is_remove = 0 and oru.org_id = ?";
		return super.findEntitys(sql, ActOrgRoleUserEntity.class, userNo,orgId);
	}

	@Override
	public List<User> getUserWithRoleCode(String roleCode) {
		String sql = "SELECT e.employee_no no,e.employee_namecn cname,e.email from act_org_role_user oru,o2o_employee e,o2o_act_role r "
				+ " where oru.user_no = e.employee_no and oru.role_id = r.id and oru.is_remove = 0 and r.role_code = ?";
		return super.findEntitys(sql, User.class, roleCode);
	}
	
	
	public List<User> getUserWithUnitNoAndRoleCode(String unitNo, List<String> roleCodeList){
		String sql = "SELECT e.employee_no no,e.employee_namecn cname,e.email,e.employee_code code,e.is_dimission status from act_org_role_user oru,o2o_employee e,o2o_act_role r,act_org o "
				+ " where oru.user_no = e.employee_no and oru.role_id = r.id and oru.is_remove = 0 and oru.org_id = o.id "
				+ " and o.unit_no = ? and r.role_code in ";
		StringBuffer roleCodeBuf = new StringBuffer("(");
		for (String roleCode : roleCodeList) {
			roleCodeBuf.append("'").append(roleCode).append("'").append(",");
		}
		sql = sql + roleCodeBuf.substring(0, roleCodeBuf.length() - 1) + ")";
		return super.findEntitys(sql, User.class,unitNo);
	}
}
